//
//  SXImageViewPage.m
//  iOS_Router_Runtime
//
//  Created by 王水鑫 on 2018/12/4.
//  Copyright © 2018年 王水鑫. All rights reserved.
//

#import "SXImageViewPage.h"

@interface SXImageViewPage ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIPageControl *pageControl;

@end

@implementation SXImageViewPage

// 初始化组件
- (instancetype)initComponentFrame:(CGRect)frame WithImages:(NSArray<UIImage *> *)images WithType:(SXImageViewPage_ComponentType)componentType {
    if (self = [super initWithFrame:frame]) {
        switch (componentType) {
            case SXImageViewPage_LaunchImageViewSimple:
            {
                [self initLaunchImageViewSimpleContentViewWithImages:images];
            }
                break;
            case SXImageViewPage_LaunchImageViewDefault:
            {
                
            }
                break;
            default:
                break;
        }
    }
    return self;
}

// 初始化type:LaunchImageViewSimple显示区域
- (void)initLaunchImageViewSimpleContentViewWithImages:(NSArray *)images {
    UIScrollView *imageViewPageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    imageViewPageScrollView.delegate = self;
    imageViewPageScrollView.pagingEnabled = YES;
    imageViewPageScrollView.bounces = NO;
    imageViewPageScrollView.showsVerticalScrollIndicator = NO;
    imageViewPageScrollView.showsHorizontalScrollIndicator = NO;
    // 内容区域大小设置
    imageViewPageScrollView.contentSize = CGSizeMake(images.count * self.frame.size.width, self.frame.size.height);
    imageViewPageScrollView.contentOffset = CGPointMake(0, 0);
    [self addSubview:imageViewPageScrollView];
    // 添加图片
    [self launchImageViewSimpleAddImageViewWithImages:images SuperView:imageViewPageScrollView];
    // 页码显示
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.frame = CGRectMake(0, self.frame.size.height - 50, self.frame.size.width, 20);
    pageControl.numberOfPages = images.count;
    pageControl.currentPage = 0;
    pageControl.userInteractionEnabled = NO;
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    [self addSubview:pageControl];
    self.pageControl = pageControl;
}
- (void)launchImageViewSimpleAddImageViewWithImages:(NSArray *)images SuperView:(UIView *)superView{
    for (int index = 0; index < images.count; index ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:images[index]];
        imageView.frame = CGRectMake(self.frame.size.width * index, 0, self.frame.size.width, self.frame.size.height);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [superView addSubview:imageView];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if(offset.x<=0){
        offset.x = 0;
        scrollView.contentOffset = offset;
    }
    NSUInteger index = round(offset.x / scrollView.frame.size.width);
    self.pageControl.currentPage = index;
}

@end
